baltabfunc_patient <- function(cont_vars, binary_vars, before_cases, after_cases, before_controls, after_controls, bal_tab_vars){
  
  
  # cont_vars = con_vars;
  # binary_vars = bin_vars;
  # before_controls=new_exp[which(new_exp$new_surgeon_flag==1 & new_exp$compound_board==0),];
  # after_controls=non_fellow_pairs[which(non_fellow_pairs$new_surgeon_flag==1 & non_fellow_pairs$compound_board==0),which(names(non_fellow_pairs) %in% names(new_exp))];
  # before_cases = new_exp[which(new_exp$new_surgeon_flag==1 & new_exp$compound_board==1),];
  # after_cases = fellow_pairs[which(fellow_pairs$new_surgeon_flag==1),which(names(fellow_pairs) %in% names(new_exp))];
  # bal_tab_vars = all_vars;
  
  ad <- NULL
  
for (i in 1:length(bal_tab_vars)){
  
  variable_name <- bal_tab_vars[i]
  
  bin_vars <- binary_vars
  con_vars <- cont_vars
  
  # before_cases <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==1),]
  # before_controls <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==0),]
  # after_cases <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==1),]
  # after_controls <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==0),]
  
  before_data <- rbind(before_cases, before_controls)
  after_data <- rbind(after_cases, after_controls)
  
  
  mean_before_cases <- mean(before_cases[,variable_name], na.rm=T)
  mean_before_controls <- mean(before_controls[,variable_name], na.rm=T)
  mean_after_cases <- mean(after_cases[,variable_name], na.rm=T)
  mean_after_controls <- mean(after_controls[,variable_name], na.rm=T)
  
  
  
  
  before_std_cases <- sd(before_cases[,variable_name], na.rm=T)
  before_std_controls <- sd(before_controls[,variable_name], na.rm=T)
  
  diffave_before <- (mean_before_cases - mean_before_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
  diffave_after <- (mean_after_cases - mean_after_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
  
  
  
  
  if(variable_name %in% bin_vars) {
    
    if(sum(is.na(before_cases[,variable_name]))<nrow(before_cases) | sum(is.na(before_controls[,variable_name]))<nrow(before_controls) | 
       sum(is.na(after_cases[,variable_name]))<nrow(after_cases) | sum(is.na(after_controls[,variable_name]))<nrow(after_controls)){
    
    ft_table <- table(before_data$treatment, before_data[,variable_name])
    ft_table
    
    N_before_cases <- sum(before_cases[,variable_name], na.rm=T)
    N_after_cases <- sum(after_cases[,variable_name], na.rm=T)
    N_after_controls <- sum(after_controls[,variable_name], na.rm=T)
    N_before_controls <- sum(before_controls[,variable_name], na.rm=T)
    
    if(sum(dim(ft_table) == c(2,2))==2) {
      
      ft_before <- fisher.test(x=ft_table)
      before_pval <- ft_before$p.value
      
    } else {
      
      before_pval <- NA
      
    }
    
    
    
    ft_table <- table(after_data$treatment, after_data[,variable_name])
    ft_table
    
    if(sum(dim(ft_table) == c(2,2))==2) {
      
      ft_after <- fisher.test(x=ft_table)
      after_pval <- ft_after$p.value
      
    } else {
      
      after_pval <- NA
      
    }
    
    } else{
      
      
      N_before_cases <- NA
      N_after_cases <- NA
      N_before_controls <- NA
      N_after_controls <- NA
      after_pval <- NA
      before_pval <- NA
      
      
      
    }
    
    
  }
  
  if(variable_name %in% con_vars) {
    
    if(sum(is.na(before_cases[,variable_name]))<nrow(before_cases) & sum(is.na(before_controls[,variable_name]))<nrow(before_controls) & 
        sum(is.na(after_cases[,variable_name]))<nrow(after_cases) & sum(is.na(after_controls[,variable_name]))<nrow(after_controls)){
      
    wt_before <- wilcox.test(x=before_cases[,variable_name], y=before_controls[,variable_name], exact=F, correct=FALSE)
    before_pval <- wt_before$p.value
    
    wt_after <-  wilcox.test(x=after_cases[,variable_name], y=after_controls[,variable_name], exact=F, correct=FALSE)
    after_pval <- wt_after$p.value
    
    N_before_cases <- dim(before_cases)[1]
    # N_after_cases <- dim(after_cases)[1]
    N_after_cases <- nrow(after_cases[which(!is.na(after_cases[,variable_name])),])
    # N_after_controls <- dim(after_controls)[1]
    N_after_controls <- nrow(after_controls[which(!is.na(after_controls[,variable_name])),])
    N_before_controls <- dim(before_controls)[1]
    
    } else{
      N_before_cases <- NA
      N_after_cases <- NA
      N_after_controls <- NA
      N_before_controls <- NA
      wt_before <- NA
      before_pval <- NA
      wt_after <- NA
      after_pval <- NA
      
    }
    
  }
  
  
  vr <- c(N_before_cases, N_after_cases, N_after_controls, N_before_controls, mean_before_cases, mean_after_cases,mean_after_controls, mean_before_controls,
          diffave_before, before_pval, diffave_after, after_pval)
  
  ad <- rbind(ad, vr)
  
  print(variable_name)
  
  
}

rownames(ad) <- bal_tab_vars
colnames(ad) <- c("N new patients before", "N new patients after",
                  "N exp patients after", "N exp patients before",
                  "mean new patients before", "mean new patients after",
                  "mean exp patients after", "mean exp patients before", "diffave before", "before pval", "diffave after", "after pval")
ad <- round(ad, 4)

number <- c(dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
            dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
            dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1])

ad <- rbind(number,ad)
rownames(ad)[1] <- "number"

return(ad)

}





baltabfunc_surgeon <- function(cont_vars, binary_vars, before_cases, after_cases, before_controls, after_controls, bal_tab_vars){
  
  
  ad <- NULL
  
  for (i in 1:length(bal_tab_vars)){
    
    variable_name <- bal_tab_vars[i]
    
    bin_vars <- binary_vars
    con_vars <- cont_vars
    
    # before_cases <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==1),]
    # before_controls <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==0),]
    # after_cases <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==1),]
    # after_controls <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==0),]
    
    before_data <- rbind(before_cases, before_controls)
    after_data <- rbind(after_cases, after_controls)
    
    
    mean_before_cases <- mean(before_cases[,variable_name], na.rm=T)
    mean_before_controls <- mean(before_controls[,variable_name], na.rm=T)
    mean_after_cases <- mean(after_cases[,variable_name], na.rm=T)
    mean_after_controls <- mean(after_controls[,variable_name], na.rm=T)
    
    
    
    
    before_std_cases <- sd(before_cases[,variable_name], na.rm=T)
    before_std_controls <- sd(before_controls[,variable_name], na.rm=T)
    
    diffave_before <- (mean_before_cases - mean_before_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
    diffave_after <- (mean_after_cases - mean_after_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
    
    
    
    
    if(variable_name %in% bin_vars) {
      
      ft_table <- table(before_data$treatment, before_data[,variable_name])
      ft_table
      
      N_before_cases <- sum(before_cases[,variable_name], na.rm=T)
      N_after_cases <- sum(after_cases[,variable_name], na.rm=T)
      N_after_controls <- sum(after_controls[,variable_name], na.rm=T)
      N_before_controls <- sum(before_controls[,variable_name], na.rm=T)
      
      if(sum(dim(ft_table) == c(2,2))==2) {
        
        ft_before <- fisher.test(x=ft_table)
        before_pval <- ft_before$p.value
        
      } else {
        
        before_pval <- NA
        
      }
      
      
      
      ft_table <- table(after_data$treatment, after_data[,variable_name])
      ft_table
      
      if(sum(dim(ft_table) == c(2,2))==2) {
        
        ft_after <- fisher.test(x=ft_table)
        after_pval <- ft_after$p.value
        
      } else {
        
        after_pval <- NA
        
      }
      
      
    }
    
    if(variable_name %in% con_vars) {
      
      wt_before <- wilcox.test(x=before_cases[,variable_name], y=before_controls[,variable_name], exact=F, correct=FALSE)
      before_pval <- wt_before$p.value
      
      wt_after <-  wilcox.test(x=after_cases[,variable_name], y=after_controls[,variable_name], exact=F, correct=FALSE)
      after_pval <- wt_after$p.value
      
      N_before_cases <- dim(before_cases)[1]
      N_after_cases <- dim(after_cases)[1]
      N_after_controls <- dim(after_controls)[1]
      N_before_controls <- dim(before_controls)[1]
      
    }
    
    
    vr <- c(N_before_cases, N_after_cases, N_after_controls, N_before_controls, mean_before_cases, mean_after_cases,mean_after_controls, mean_before_controls,
            diffave_before, before_pval, diffave_after, after_pval)
    
    ad <- rbind(ad, vr)
    
    print(variable_name)
    
    
  }
  
  rownames(ad) <- bal_tab_vars
  colnames(ad) <- c("N new surgeons before", "N new surgeons after",
                    "N exp surgeons after", "N exp surgeons before",
                    "mean new surgeons before", "mean new surgeons after",
                    "mean exp surgeons after", "mean exp surgeons before", "diffave before", "before pval", "diffave after", "after pval")
  ad <- round(ad, 4)
  
  number <- c(dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
              dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
              dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1])
  
  ad <- rbind(number,ad)
  rownames(ad)[1] <- "number"
  
  return(ad)
  
}



baltabfunc_just_cont <- function(cont_vars, before_cases, after_cases, before_controls, after_controls, bal_tab_vars){
  
  
  ad <- NULL
  
  for (i in 1:length(bal_tab_vars)){
    
    variable_name <- bal_tab_vars[i]
    con_vars <- cont_vars
    
    # before_cases <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==1),]
    # before_controls <- atdata_black_nonh[which(atdata_black_nonh$black_nonh==0),]
    # after_cases <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==1),]
    # after_controls <- all_exact_groups_black_nonh[which(all_exact_groups_black_nonh$black_nonh==0),]
    
    
    before_data <- rbind(before_cases, before_controls)
    after_data <- rbind(after_cases, after_controls)
    
    
    mean_before_cases <- mean(before_cases[,variable_name], na.rm=T)
    mean_before_controls <- mean(before_controls[,variable_name], na.rm=T)
    mean_after_cases <- mean(after_cases[,variable_name], na.rm=T)
    mean_after_controls <- mean(after_controls[,variable_name], na.rm=T)
    
    
    
    
    before_std_cases <- sd(before_cases[,variable_name], na.rm=T)
    before_std_controls <- sd(before_controls[,variable_name], na.rm=T)
    
    diffave_before <- (mean_before_cases - mean_before_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
    diffave_after <- (mean_after_cases - mean_after_controls) / sqrt((before_std_cases^2+before_std_controls^2)/2)
    
    
    
    
    # if(variable_name %in% bin_vars) {
    #   
    #   ft_table <- table(before_data$treatment, before_data[,variable_name])
    #   ft_table
    #   
    #   N_before_cases <- sum(before_cases[,variable_name])
    #   N_after_cases <- sum(after_cases[,variable_name])
    #   N_after_controls <- sum(after_controls[,variable_name])
    #   N_before_controls <- sum(before_controls[,variable_name])
    #   
    #   if(sum(dim(ft_table) == c(2,2))==2) {
    #     
    #     ft_before <- fisher.test(x=ft_table)
    #     before_pval <- ft_before$p.value
    #     
    #   } else {
    #     
    #     before_pval <- NA
    #     
    #   }
    #   
    #   
    #   
    #   ft_table <- table(after_data$treatment, after_data[,variable_name])
    #   ft_table
    #   
    #   if(sum(dim(ft_table) == c(2,2))==2) {
    #     
    #     ft_after <- fisher.test(x=ft_table)
    #     after_pval <- ft_after$p.value
    #     
    #   } else {
    #     
    #     after_pval <- NA
    #     
    #   }
    #   
    #   
    # }
    
    if(variable_name %in% con_vars) {
      
      wt_before <- wilcox.test(x=before_cases[,variable_name], y=before_controls[,variable_name], exact=F, correct=FALSE)
      before_pval <- wt_before$p.value
      
      wt_after <-  wilcox.test(x=after_cases[,variable_name], y=after_controls[,variable_name], exact=F, correct=FALSE)
      after_pval <- wt_after$p.value
      
      N_before_cases <- dim(before_cases)[1]
      N_after_cases <- dim(after_cases)[1]
      N_after_controls <- dim(after_controls)[1]
      N_before_controls <- dim(before_controls)[1]
      
    }
    
    
    vr <- c(N_before_cases, N_after_cases, N_after_controls, N_before_controls, mean_before_cases, mean_after_cases,mean_after_controls, mean_before_controls,
            diffave_before, before_pval, diffave_after, after_pval)
    
    ad <- rbind(ad, vr)
    
    print(variable_name)
    
    
  }
  
  rownames(ad) <- bal_tab_vars
  colnames(ad) <- c("N new patients before", "N new patients after",
                    "N exp patients after", "N exp patients before",
                    "mean new patients before", "mean new patients after",
                    "mean exp patients after", "mean exp patients before", "diffave before", "before pval", "diffave after", "after pval")
  ad <- round(ad, 4)
  
  number <- c(dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
              dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1],
              dim(before_cases)[1], dim(after_cases)[1], dim(after_controls)[1], dim(before_controls)[1])
  
  ad <- rbind(number,ad)
  rownames(ad)[1] <- "number"
  
  return(ad)
  
}